<?php
if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' ); 
/**
* @version $Id: ps_paybox.php,v 2.0 2005/09/15 15:06:39 ffab Exp $
* @package mambo-phpShop
* @subpackage Payment
* @author     Fabrice Fetsch <fabrice.fetsch(at)keoconcept.com>
* @copyright (C) 2004-2005 Soeren Eberhardt
*
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
* mambo-phpShop is Free Software.
* mambo-phpShop comes with absolute no warranty.
*
* www.mambo-phpshop.net
*
* The ps_paybox class.
*
*/

class ps_paybox {

	var $classname = "ps_paybox";
	var $payment_code = "PAYBOX";

	/**
    * Show all configuration parameters for this payment method
    * @returns boolean False when the Payment method has no configration
    */
	function show_configuration() {
		global $VM_LANG, $mosConfig_live_site, $mosConfig_lang;

		$PAYBOX_LANG =& new ps_paybox_Language();
		/** Read current Configuration ***/
		require_once(CLASSPATH ."payment/".$this->classname.".cfg.php");
		$db = &new ps_DB;

    ?>
    <table>
        <tr>
        <td><strong><?php echo $PAYBOX_LANG->_('PS_PAYBOX_ADMIN_CFG_PAYBOX_EMAIL'); ?></strong></td>
            <td>
                <input type="text" name="PAYBOX_EMAIL" class="inputbox" size="30"  value="<?php echo PAYBOX_EMAIL ?>" />
            </td>
            <td><?php echo $PAYBOX_LANG->_('PS_PAYBOX_ADMIN_CFG_PAYBOX_EMAIL_EXPLAIN'); ?>
            </td>
        </tr>
        <tr>
            <td><strong><?php echo $PAYBOX_LANG->_('PS_PAYBOX_ADMIN_CFG_PAYBOX_SITE_NUMBER'); ?></strong></td>
            <td>
                <input type="text" name="PAYBOX_IBS_SITE" class="inputbox"  size="30" value="<?php  echo PAYBOX_IBS_SITE ?>" />
            </td>
            <td><?php echo $PAYBOX_LANG->_('PS_PAYBOX_ADMIN_CFG_PAYBOX_SITE_NUMBER_EXPLAIN'); ?></td>
        </tr>
        <tr>
            <td><strong><?php echo $PAYBOX_LANG->_('PS_PAYBOX_ADMIN_CFG_PAYBOX_RANK_NUMBER'); ?></strong></td>
            <td>
                <input type="text" name="PAYBOX_IBS_RANK" class="inputbox"size="30"  value="<?php  echo PAYBOX_IBS_RANK ?>" />
            </td>
            <td><?php echo $PAYBOX_LANG->_('PS_PAYBOX_ADMIN_CFG_PAYBOX_RANK_NUMBER_EXPLAIN'); ?></td>
        </tr>
        <tr>
            <td><strong><?php echo $PAYBOX_LANG->_('PS_PAYBOX_ADMIN_CFG_PAYBOX_ID'); ?></strong></td>
            <td>
                <input type="text" name="PAYBOX_ID" class="inputbox"size="30"  value="<?php  echo PAYBOX_ID ?>" />
            </td>
            <td><?php echo $PAYBOX_LANG->_('PS_PAYBOX_ADMIN_CFG_PAYBOX_ID_EXPLAIN'); ?></td>
        </tr>
        <tr>
            <td><strong><?php echo $PAYBOX_LANG->_('PS_PAYBOX_ADMIN_CFG_PAYBOX_URL_CGI'); ?></strong></td>
            <td>
                <input type="text" name="PAYBOX_MOD_URL" class="inputbox"size="30"  value="<?php  echo PAYBOX_MOD_URL ?>" />
            </td>
            <td><?php echo $PAYBOX_LANG->_('PS_PAYBOX_ADMIN_CFG_PAYBOX_URL_CGI_EXPLAIN'); ?></td>
        </tr>
        <tr>
            <td><strong><?php echo $PAYBOX_LANG->_('PS_PAYBOX_ADMIN_CFG_PAYBOX_LANG'); ?></strong></td>
            <td>
                <input type="text" name="PAYBOX_LANG" class="inputbox"size="30"  value="<?php  echo PAYBOX_LANG ?>" />
            </td>
            <td><?php echo $PAYBOX_LANG->_('PS_PAYBOX_ADMIN_CFG_PAYBOX_LANG_EXPLAIN'); ?></td>
        </tr>
        <tr>
            <td><strong><?php echo $PAYBOX_LANG->_('PS_PAYBOX_ADMIN_CFG_PAYBOX_CURRENCY'); ?></strong></td>
            <td>
                <input type="text" name="PAYBOX_CURRENCY" class="inputbox"size="30"  value="<?php  echo PAYBOX_CURRENCY ?>" />
            </td>
            <td><?php echo $PAYBOX_LANG->_('PS_PAYBOX_ADMIN_CFG_PAYBOX_CURRENCY_EXPLAIN'); ?></td>
        </tr>
         
        <tr>
        <td><strong><?php echo $PAYBOX_LANG->_('PS_PAYBOX_ADMIN_CFG_PAYBOX_PREFIX'); ?></strong></td>
            <td>
                <input type="text" name="PAYBOX_PREFIX" class="inputbox" size="30"  value="<?php echo PAYBOX_PREFIX ?>" />
            </td>
            <td><?php echo $PAYBOX_LANG->_('PS_PAYBOX_ADMIN_CFG_PAYBOX_PREFIX_EXPLAIN'); ?>
            </td>
        </tr>
         
        <tr>
            <td><strong><?php echo $PAYBOX_LANG->_('PS_PAYBOX_ADMIN_CFG_PAYBOX_STATUS_SUCCESS'); ?></strong></td>
            <td>
                <select name="PAYBOX_VERIFIED_STATUS" class="inputbox" >
                <?php
                $q = "SELECT order_status_name,order_status_code FROM #__{vm}_order_status ORDER BY list_order";
                $db->query($q);
                $order_status_code = Array();
                $order_status_name = Array();

                while ($db->next_record()) {
                	$order_status_code[] = $db->f("order_status_code");
                	$order_status_name[] =  $db->f("order_status_name");
                }
                for ($i = 0; $i < sizeof($order_status_code); $i++) {
                	echo "<option value=\"" . $order_status_code[$i];
                	if (PAYBOX_VERIFIED_STATUS == $order_status_code[$i])
                	echo "\" selected=\"selected\">";
                	else
                	echo "\">";
                	echo $order_status_name[$i] . "</option>\n";
                    }?>
                    </select>
            </td>
            <td><?php echo $PAYBOX_LANG->_('PS_PAYBOX_ADMIN_CFG_PAYBOX_STATUS_SUCCESS_EXPLAIN'); ?>
            </td>
        </tr>
            <tr>
            <td><strong><?php echo $PAYBOX_LANG->_('PS_PAYBOX_ADMIN_CFG_PAYBOX_STATUS_FAILED'); ?></strong></td>
            <td>
                <select name="PAYBOX_INVALID_STATUS" class="inputbox" >
                <?php
                for ($i = 0; $i < sizeof($order_status_code); $i++) {
                	echo "<option value=\"" . $order_status_code[$i];
                	if (PAYBOX_INVALID_STATUS == $order_status_code[$i])
                	echo "\" selected=\"selected\">";
                	else
                	echo "\">";
                	echo $order_status_name[$i] . "</option>\n";
                    } ?>
                    </select>
            </td>
            <td><?php echo $PAYBOX_LANG->_('PS_PAYBOX_ADMIN_CFG_PAYBOX_STATUS_FAILED_EXPLAIN'); ?>
            </td>
        </tr>
       <tr>
         <td><strong><?php echo $PAYBOX_LANG->_('PS_PAYBOX_ADMIN_CFG_PAYBOX_EMAIL_CONFIRM'); ?></strong></td>
         <td>
          <input type="checkbox" name="PAYBOX_SEND_CONFIRM" class="inputbox"size="30"  <?php if (PAYBOX_SEND_CONFIRM == '1') echo "checked"; ?> value="1" />
         </td>
         <td><?php echo $PAYBOX_LANG->_('PS_PAYBOX_ADMIN_CFG_PAYBOX_EMAIL_CONFIRM_EXPLAIN'); ?></td>
        </tr>
       <tr>
         <td><strong><?php echo $PAYBOX_LANG->_('PS_PAYBOX_ADMIN_CFG_PAYBOX_EMAIL_ERROR'); ?></strong></td>
         <td>
          <input type="checkbox" name="PAYBOX_SEND_ERROR" class="inputbox"size="30"  <?php if (PAYBOX_SEND_ERROR == '1') echo "checked"; ?> value="1" />
         </td>
         <td><?php echo $PAYBOX_LANG->_('PS_PAYBOX_ADMIN_CFG_PAYBOX_EMAIL_ERROR_EXPLAIN'); ?></td>
        </tr>
      </table>
    <?php
	}

	function has_configuration() {
		// return false if there's no configuration
		return true;
	}

	/**
	* Returns the "is_writeable" status of the configuration file
	* @param void
	* @returns boolean True when the configuration file is writeable, false when not
	*/
	function configfile_writeable() {
		return is_writeable( CLASSPATH."payment/".$this->classname.".cfg.php" );
	}

	/**
	* Returns the "is_readable" status of the configuration file
	* @param void
	* @returns boolean True when the configuration file is writeable, false when not
	*/
	function configfile_readable() {
		return is_readable( CLASSPATH."payment/".$this->classname.".cfg.php" );
	}

	/**
	* Writes the configuration file for this payment method
	* @param array An array of objects
	* @returns boolean True when writing was successful
	*/
	function write_configuration( &$d ) {
		global $mosConfig_absolute_path, $mosConfig_live_site;
		$my_config_array = array(
		"PAYBOX_EMAIL"          =>      $d['PAYBOX_EMAIL'],
		"PAYBOX_IBS_SITE"	=> 	$d['PAYBOX_IBS_SITE'],
		"PAYBOX_IBS_RANK"	=> 	$d['PAYBOX_IBS_RANK'],
		"PAYBOX_ID"	        => 	$d['PAYBOX_ID'],
		"PAYBOX_MOD_URL"	=> 	$d['PAYBOX_MOD_URL'],
		"PAYBOX_LANG"	    	=> 	$d['PAYBOX_LANG'],
		"PAYBOX_CURRENCY"	=> 	$d['PAYBOX_CURRENCY'],
		"PAYBOX_PREFIX"          =>     $d['PAYBOX_PREFIX'],
		"PAYBOX_VERIFIED_STATUS"=> 	$d['PAYBOX_VERIFIED_STATUS'],
		"PAYBOX_INVALID_STATUS"	=> 	$d['PAYBOX_INVALID_STATUS'],
		"PAYBOX_SEND_CONFIRM"   =>      $d['PAYBOX_SEND_CONFIRM'],
		"PAYBOX_SEND_ERROR"     =>      $d['PAYBOX_SEND_ERROR']
		);
		$config = "<?php\n";
		$config .= "if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' ); ";
		foreach( $my_config_array as $key => $value ) {
			$config .= "define ('$key', '$value');\n";
		}

		$config .= "?>";

		if ($fp = fopen(CLASSPATH ."payment/".$this->classname.".cfg.php", "w")) {
			fputs($fp, $config, strlen($config));
			fclose ($fp);

		}
		else
		return false;


		// Mixing POST FORM AN DLOCAL FILE WITH IMPORTANT INFO
		$config = "# identifiant du site \n";
		$config .= "PBX_SITE=".$d['PAYBOX_IBS_SITE']."\n";
		$config .= "PBX_RANG=".$d['PAYBOX_IBS_RANK']."\n";
		$config .= "PBX_IDENTIFIANT=".$d['PAYBOX_ID']."\n";


		if ($fp = fopen(CLASSPATH ."payment/".$this->classname."_local.txt", "w")) {
			fputs($fp, $config, strlen($config));
			fclose ($fp);
			return true;
		}
		else
		return false;
	}

	/**************************************************************************
	** name: process_payment()
	** returns:
	***************************************************************************/
	function process_payment($order_number, $order_total, &$d) {
		return true;
	}

}

class ps_paybox_Language extends vmAbstractLanguage {
	function ps_paybox_Language() {
		global $mosConfig_lang;

		if (file_exists( ADMINPATH. 'languages/paybox/'.$mosConfig_lang.'.php' )) {
			require_once( ADMINPATH. 'languages/paybox/'.$mosConfig_lang.'.php' );
		} else if (file_exists( ADMINPATH. 'languages/paybox/french.php' )) {
			require_once( ADMINPATH. 'languages/paybox/french.php' );
		}
		$this->setDebug();
		$this->initModule( 'paybox', $langvars );

	}
	/**
	* Translator function
	* @param string Name of the Class Variable
	* @param boolean Encode String to HTML entities?
	* @return string The value of $var (as an HTML Entitiy-encoded string if $htmlentities)
	*/
	function _( $var, $htmlentities=false ) {

		$module = 'paybox';
		$key = strtoupper( $var );
		// if language module not yet loaded, load now
		if (!isset($this->modules[$module])) {
			$this->load($module);
		}
		$text = false;
		if (isset($this->modules[$module][$key])) {
			$text = $this->modules[$module][$key];
		} elseif (isset($this->modules['common'][$key])) {
			$text = $this->modules['common'][$key];
			$module = 'common';
		}
		if ($text!==false) {
			if( $htmlentities ) {
				$text = htmlentities( $text, ENT_QUOTES, $this->getCharset($module));
				// some symbols are not converted correctly... doing manually
				$text = str_replace(chr(128),'&euro;',$text);
				// enable the use of HTML tags in language file... is this really good?
				$text = str_replace('&lt;','<',$text);
				$text = str_replace('&gt;','>',$text);
				return $text;
			} else {
				$text = $this->convert($text,$module);
				return stripslashes( $text );
			}
		} elseif( $this->_debug ) {
			return "$var is missing in language file.";
		} else {
			return $var;
		}
	}
}
?>